package com.example.handler;

import com.alibaba.fastjson2.JSON;
import com.example.domain.ResponseCode;
import com.example.domain.ResponseResult;
import com.example.utils.WebUtil;
import jakarta.servlet.ServletException;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;
import org.springframework.http.HttpStatus;
import org.springframework.security.core.AuthenticationException;
import org.springframework.security.web.AuthenticationEntryPoint;
import org.springframework.stereotype.Component;

import java.io.IOException;

/**
 * 认证失败处理类
 */
@Component
public class AuthenticationEntryPointImpl implements AuthenticationEntryPoint {
    @Override
    public void commence(HttpServletRequest request, HttpServletResponse response, AuthenticationException authException) throws IOException, ServletException {
        ResponseResult result = new ResponseResult(
                ResponseCode.AUTHENTICATION_ERROR,
                "认证失败",
                null
        );
        // 处理异常
        WebUtil.renderString(response, JSON.toJSONString(result));
    }
}
